package com.yej.aiPolice.ui.adpter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.daimajia.slider.library.SliderLayout
import com.daimajia.slider.library.SliderTypes.TextSliderView
import com.squareup.picasso.Picasso
import com.yej.aiPolice.R
import com.yej.aiPolice.model.beans.Denews
import com.yej.aiPolice.ui.activity.NewsActivity
import com.yej.aiPolice.ui.activity.WebActivity
import com.yej.aiPolice.ui.activity.web.IMActivity
import com.yej.aiPolice.util.AiPoliceAPP
import org.jetbrains.anko.find
import java.text.SimpleDateFormat


class HomeRVAdapter(val context: Context?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    //虚拟数据
    var mDatas : List<Denews> = ArrayList()

    //Kotlin定义常量
    companion object{
        val TYPE_TITLE = 0
        val TYPE_ARTICLE = 1
    }
    /**
     * 不同position'对应不同类型
     */
    override fun getItemViewType(position: Int): Int {
        if(position == 0){
            return TYPE_TITLE
        }else{
            return TYPE_ARTICLE
        }
    }

    fun setData( newsList:List<Denews> ){
        mDatas = newsList
        notifyDataSetChanged()
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when(viewType){
            TYPE_TITLE -> return TitleHolder(View.inflate(context, R.layout.item_title, null))
            TYPE_ARTICLE-> return ArticleHolder(View.inflate(context, R.layout.item_seller, null))
            else -> return TitleHolder(View.inflate(context, R.layout.item_seller, null))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val viewType = getItemViewType(position)
        when(viewType){
            TYPE_TITLE -> (holder as TitleHolder).bindData("我是头布局=====")
            TYPE_ARTICLE -> (holder as ArticleHolder).bindData(mDatas[position-1])
        }
    }

    override fun getItemCount(): Int {
        if(mDatas.size > 0){
            return mDatas.size + 1
        }else{
            return 0
        }

    }

    var url_maps : HashMap<String,String> = HashMap()
    inner class TitleHolder(item:View) : RecyclerView.ViewHolder(item){

        val sliderLayout:SliderLayout
        val iv_im_enter:ImageView
        val iv_enter_1:ImageView
        val iv_enter_2:ImageView
        val iv_enter_3:ImageView
        val iv_enter_4:ImageView
        val iv_enter_5:ImageView
        val iv_enter_6:ImageView
        val iv_enter_7:ImageView
        val iv_enter_8:ImageView

        init {
            sliderLayout = item.find(R.id.slider)
            iv_im_enter = item.find(R.id.iv_im_enter)
            iv_enter_1 = item.find(R.id.iv_enter_1)
            iv_enter_2 = item.find(R.id.iv_enter_2)
            iv_enter_3 = item.find(R.id.iv_enter_3)
            iv_enter_4 = item.find(R.id.iv_enter_4)
            iv_enter_5 = item.find(R.id.iv_enter_5)
            iv_enter_6 = item.find(R.id.iv_im_enter)
            iv_enter_7 = item.find(R.id.iv_enter_7)
            iv_enter_8 = item.find(R.id.iv_enter_8)

            iv_im_enter.setOnClickListener {
                val intent:Intent = Intent(context, IMActivity::class.java);
                context?.startActivity(intent)
            }


            iv_enter_1.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",1);
                context?.startActivity(intent)
            }

            iv_enter_2.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",2);
                context?.startActivity(intent)
            }
            iv_enter_3.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",3);
                context?.startActivity(intent)
            }
            iv_enter_4.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",4);
                context?.startActivity(intent)
            }
            iv_enter_5.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",5);
                context?.startActivity(intent)
            }
            iv_enter_6.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",6);
                context?.startActivity(intent)
            }
            iv_enter_7.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",7);
                context?.startActivity(intent)
            }
            iv_enter_8.setOnClickListener {
                val intent:Intent = Intent(context, WebActivity::class.java);
                intent.putExtra("urlId",8);
                context?.startActivity(intent)
            }





        }
        fun bindData(data: String) {
            if (url_maps.size == 0){
                url_maps.put("从人民中汲取磅礴力量——习近平总书记同出席2020年全国两会人大代表、政协委员共商国是纪实", "https://img.xmnn.cn/003/004/536/00300453610_b3f28d86.jpg")
                url_maps.put("十三届全国人大三次会议举行闭幕会", "https://img.xmnn.cn/003/004/535/00300453544_cbd9d209.jpg")
                url_maps.put("李克强出席记者会并回答中外记者提问", "http://www.xinhuanet.com/politics/2020lh/2020-05/28/1126045689_15906537401571n.jpg")
                url_maps.put("美欲驱逐3000中国学生？我学者：可对等反制", "https://i.guancha.cn/news/2020/05/29/20200529134431621.jpg")

                for((key,value) in url_maps){
                    var textSliderView:TextSliderView = TextSliderView(context)
                    textSliderView.description(key).image(value)
                    sliderLayout.addSlider(textSliderView)
                }
            }

        }
    }

    inner class ArticleHolder(item:View) : RecyclerView.ViewHolder(item){
        val tvTitle : TextView
        val newsLogo : ImageView
        val newsContent : TextView
        val newsOrigin : TextView
        val newTime :TextView
        val newsItem:LinearLayout
        init {
            tvTitle = item.find(R.id.tv_title)
            newsLogo = item.find(R.id.new_logo)
            newsContent = item.find(R.id.new_content)
            newsOrigin = item.find(R.id.new_origin)
            newTime = item.find(R.id.new_time)
            newsItem = item.find(R.id.ll_news_item)

        }
        fun bindData(data:Denews) {
            tvTitle.text = data.title
            newsContent.text = data.describe
            newsOrigin.text = "智慧警务"
            val time: String = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data.date)
            newTime.text = time
            var picUrl:String = AiPoliceAPP.HOSTURL+"/img/news/"+data.id+".jpg"
            Log.e("Home","pICURL："+picUrl)
            //TODO:赋值其他字段后面再处理
            Picasso.with(context).load(picUrl).into(newsLogo)
            newsItem.setOnClickListener {
                val intent:Intent = Intent(context, NewsActivity::class.java);
                intent.putExtra("newsId",data.conid);
                context?.startActivity(intent)
            }

        }


    }
}
